<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Api extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('api_model');
	}

	public function ewan(){
		$data = array(':p_year_month' => "");
		$this->api_model->gen_table($data);
	}


	 public function sync_userdatus(){
	 	echo $this->itexmo('Gee Ronalds Actub','PERS','Actub');
	 }

	 
	 public function call_all(){
	 	$this->send_users();
	 	$this->send_billing_report();
	 	$this->send_collection_report();
	 }

	 public function send_users(){
	 	$url = 'http://wumsdashboard.com/api/get_users_send';
	    $data = $this->api_model->get_users();
	    $ch = curl_init( $url );
		# Setup request to send json via POST.
		$payload = json_encode($data);
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
		# Return response instead of printing.
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		# Send request.
		$result = curl_exec($ch);
		curl_close($ch);
		echo $result;
		return $result;
	}

	public function get_data_online(){
		$url = 'http://myonlinetracker.com/sync/get_reads';
	    //$data = $this->api_model->get_users();
	    $ch = curl_init( $url );
		# Setup request to send json via POST.
		//$payload = json_encode($data);
		//curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
		# Return response instead of printing.
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		# Send request.
		$result = curl_exec($ch);
		curl_close($ch);
		
        $data = json_decode($result);
        if(!empty($data)){
        	foreach ($data as $d) {


		$data2 = array(
				'account_id' 	=> $d->account_id,
				'prev_read' 	=> $d->prev_read,
				'prev_date' 	=> $d->prev_date,
				'meter_reading' => $d->meter_reading,
				'reading_date' 	=> $d->reading_date,
				'consumed' 		=> $d->consumed,
				'bill_date' 	=> $d->bill_date,
				'bill_month' 	=> $d->bill_month,
				'bill_year' 	=> $d->bill_year,
				'read_amt' 		=> $d->read_amt,
				'bill_bal' 		=> $d->bill_bal,
				'curchg' 		=> $d->curchg,
				'total_due' 	=> $d->total_due,
				'due_date' 		=> $d->due_date,
				'print_flag' 	=> $d->print_flag,
				'print_by' 		=> $d->print_by,
				'print_date' 	=> $d->print_date,
				'sp_flag' 		=> $d->sp_flag,
				'sp_readType' 	=> $d->sp_readType,
				'sp_reason'		=> $d->sp_reason,
				'rates'			=> $d->rates,
				'adv_pay'		=> $d->adv_pay,
				'remarks'		=> $d->remarks,
				'findings'		=> $d->findings
			);

		    $res = $this->api_model->sync_bills($data2);
		    foreach ($res as $key) {
				$c = $key->status;
			}

			if ($c == TRUE) {
				$this->update_online_read_tbl($d->reading_id,$d->account_id,'Y');
			} else {
				$this->update_online_read_tbl($d->reading_id,$d->account_id,'N');
			}

        	//$this->update_online_read_tbl($d->reading_id,$d->account_id);
        }
        }
  //       foreach ($data as $d) {


		// $data2 = array(
		// 		'account_id' 	=> $d->account_id,
		// 		'prev_read' 	=> $d->prev_read,
		// 		'prev_date' 	=> $d->prev_date,
		// 		'meter_reading' => $d->meter_reading,
		// 		'reading_date' 	=> $d->reading_date,
		// 		'consumed' 		=> $d->consumed,
		// 		'bill_date' 	=> $d->bill_date,
		// 		'bill_month' 	=> $d->bill_month,
		// 		'bill_year' 	=> $d->bill_year,
		// 		'read_amt' 		=> $d->read_amt,
		// 		'bill_bal' 		=> $d->bill_bal,
		// 		'curchg' 		=> $d->curchg,
		// 		'total_due' 	=> $d->total_due,
		// 		'due_date' 		=> $d->due_date,
		// 		'print_flag' 	=> $d->print_flag,
		// 		'print_by' 		=> $d->print_by,
		// 		'print_date' 	=> $d->print_date,
		// 		'sp_flag' 		=> $d->sp_flag,
		// 		'sp_readType' 	=> $d->sp_readType,
		// 		'sp_reason'		=> $d->sp_reason,
		// 		'rates'			=> $d->rates,
		// 		'adv_pay'		=> $d->adv_pay,
		// 		'remarks'		=> $d->remarks,
		// 		'findings'		=> $d->findings
		// 	);

		//     $res = $this->api_model->sync_bills($data2);
		//     foreach ($res as $key) {
		// 		$c = $key->status;
		// 	}

		// 	if ($c == TRUE) {
		// 		$this->update_online_read_tbl($d->reading_id,$d->account_id,'Y');
		// 	} else {
		// 		$this->update_online_read_tbl($d->reading_id,$d->account_id,'N');
		// 	}

  //       	//$this->update_online_read_tbl($d->reading_id,$d->account_id);
  //       }

        //var_dump($data);

		//echo $result;
		//return $result;
	}

	public function update_online_read_tbl($id,$account,$stat){
		$url = 'http://myonlinetracker.com/sync/update_reads';
	    //$data = $this->api_model->get_users();
	    $data = array(
	    		'reading_id' => $id,
	    		'account_id' => $account,
	    		'status'	 => $stat
	    	);
	    $ch = curl_init( $url );
		# Setup request to send json via POST.
		$payload = json_encode($data);
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
		# Return response instead of printing.
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		# Send request.
		$result = curl_exec($ch);
	}


	public function send_billing_report(){
		$url = 'http://wumsdashboard.com/api/get_billing_report';
		$data = $this->api_model->get_mv_billing();
		$ch = curl_init($url);

		$payload = json_encode($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$result = curl_exec($ch);
		curl_close($ch);
		echo $result;
		return $result;
	}


	public function send_arrear_report(){
		$url = 'http://wumsdashboard.com/api/get_arrear_report';
		$data = $this->api_model->get_arrears();
		$ch = curl_init($url);

		$payload = json_encode($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$result = curl_exec($ch);
		curl_close($ch);
		echo $result;
		return $result;
	}


	public function send_cashFlow_report(){
		$url = 'http://wumsdashboard.com/api/get_cashflow_report';
		$data = $this->api_model->get_cash_flow();
		$ch = curl_init($url);

		$payload = json_encode($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$result = curl_exec($ch);
		curl_close($ch);
		echo $result;
		return $result;

	}


	public function send_sms(){
		$URL = "http://v2.smsgateway.me/API/Send/Single.php";

		$postdata = http_build_query(
		    array(
		    'Username' => "playsparkbiz@gmail.com",
		    'Password' => "bizlertech",
		    'Number' => "+639261270290",
		    'Message' => "Hello World!",
		    'Device' => 26994, //Send from device 1 (optional)
		    'SendAt' => (time() + (60 * 60)), //Send in 1 hours time (optional)
		    'ExpireAt' => (time() + (60 * 60 * 2)) //Expire in 2 hours time (optional)
		    )
		);

		$opts = array('http' => array('method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));

		$context  = stream_context_create($opts);

		$result = file_get_contents($URL, false, $context);

		echo $result;
	}

	public function send_collection_report(){
		$url = 'http://wumsdashboard.com/api/get_collection_report';
		$data = $this->api_model->get_mv_collection();
		$ch = curl_init($url);

		$payload = json_encode($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$result = curl_exec($ch);
		curl_close($ch);
		echo $result;
		return $result;

	}



}
